package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.analyse;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkKnowReport;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkKnowReportRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.HomeworkInfoAnalysePointGraphService;

/**
 * 知识点掌握程度图表
 */
@CrossOrigin
@RestController
public class HomeworkPointGraphController extends YxyApiBaseController {

    @Autowired
    HomeworkKnowReportRepository homeworkKnowReportRepository;

    @Autowired
    HomeworkInfoAnalysePointGraphService homeworkInfoAnalysePointGraphService;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workinfo/analyse/pointgraph", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam String taskId, //
            @RequestParam String classId, //
            ReqModel reqModel, //
            RespModel respModel) throws ParseException {
        // ##
        // 获取用户基本信息
        try {
            reqModel.userId = getProfile(request, response).getId();
            reqModel.schoolId = getProfile(request, response).getSchoolId();
        } catch (Exception e) {
            Assert.isTrue(false, "用户信息异常，请重新登录。");
        }
        reqModel.taskId = taskId;
        reqModel.classId = classId;
        // ##
        // 获取班级作业任务学生答题详情
        List<String> questionTypes = new ArrayList<String>();
        questionTypes.add("0");
        questionTypes.add("1");
        List<HomeworkKnowReport> homeworkKnowReports = homeworkKnowReportRepository.findByTaskIdAndClassId(taskId,
                classId);
        // 数据转化模型
        respModel = homeworkInfoAnalysePointGraphService.convertRespModel(homeworkKnowReports, taskId);
        return respModel;
    }

    /** 请求参数 **/
    public static class ReqModel extends YxyApiReqModel {
        // 任务Id
        public String taskId;
        // 班级Id
        public String classId;
        // 用户Id (不用传参)
        public String userId;
        // 学校Id (不用传参)
        public String schoolId;
    }

    /** 响应 **/
    public static class RespModel extends YxyApiRespModel {
        // 班级知识点掌握程度报表
        public List<PointSheet> pointSheets;
        // 知识点结果简评
        public PointRemark pointRemark;
    }

    // 知识点掌握程度图表
    public static class PointSheet {
        // 知识点Id
        public int knowId;
        // 知识点名称
        public String knowName;
        // 掌握比率
        public double rate;
    }

    // 知识点结果简评
    public static class PointRemark {
        // 知识点结果状态 1: 正确率低于60%，2：正确率>=60%, 正确率<=80%, 3: 正确率 >= 80%
        public int status;
        // 知识点名称数组
        public List<String> knowNames;
    }
}
